package assists.media
{
	import assists.TimeToStr;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import panel.media.TextScrollAuto;
	import flash.media.SoundLoaderContext;
	
	public class SoundMedia extends Sprite
	{
		private  var _sound:Sound;
		private var _soundControl:SoundChannel;
		private var _resumetime:uint = 0;//开始播放的时间点
		private var _onUpdate:Function = new Function();
		private var _timer:Timer;
		private var _totalTime:String;
		private var _playPos:Number;

		private var _isPause:Boolean;
		public var soundName:String;
		private var _onChangeMusic:Function;
		public function SoundMedia()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_timer = new Timer(1000);
		}
		
		public function loadSound(url:String):void
		{
//			url = "http://iweihai-app.googlecode.com/files/111.mp3";
			var buffer:SoundLoaderContext = new SoundLoaderContext(1000);
			if(!_sound)
			{
			_sound = new Sound();
			_soundControl = new SoundChannel();
			}else
			{
				_soundControl.stop();
				_sound = new Sound();
				if(_onChangeMusic != null)
				{
				_onChangeMusic();
				}
			}
			var urlrequest:URLRequest = new URLRequest(url);
			_sound.load(urlrequest,buffer);
			_soundControl = _sound.play(_resumetime);
			_totalTime = TimeToStr.timeChangeToStr(_sound.length);
			_timer.addEventListener(TimerEvent.TIMER,timerHandler);
			_timer.start();
		}
		
		
		protected function soundEndHandler(event:Event):void
		{
			this.removeEventListener(Event.SOUND_COMPLETE,soundEndHandler);
			this.removeEventListener(TimerEvent.TIMER,timerHandler);
			_timer.stop();
			
		}
		
		protected function timerHandler(event:TimerEvent = null):void
		{
//			if(_sound.length <= _soundControl.position)
//			{
//				this.removeEventListener(TimerEvent.TIMER,timerHandler);
//				_timer.stop();
//			}
			_totalTime = TimeToStr.timeChangeToStr(_sound.length);
			_onUpdate(_soundControl.position,_sound.length,TimeToStr.timeChangeToStr(_soundControl.position) + "/" + _totalTime);
		}
		
		public function updateProcess(current:uint):void
		{
			_playPos = current;
			_timer.stop();
			_soundControl.stop();
			if(_isPause) 
			{
				_onUpdate(current,_sound.length,TimeToStr.timeChangeToStr(current) + "/" + _totalTime);
				return;
			}
			_soundControl = _sound.play(current);
			timerHandler();
			_timer.start();
		}
		
		public function onControllSound(isPause:Boolean):void
		{
			_isPause = isPause;
			if(isPause)
			{
				_timer.stop();
				_playPos = _soundControl.position;
				_soundControl.stop();
			}else
			{
				_timer.start();
				_soundControl = _sound.play(_playPos);
			}
		}
		
		
		public function set onUpdate(value:Function):void
		{
			_onUpdate = value;
		}
		
		public function set changeMusic(value:Function):void
		{
			_onChangeMusic = value;
		}
		
		public function clear():void
		{
			if(_timer.hasEventListener(TimerEvent.TIMER))
			{
				this.removeEventListener(TimerEvent.TIMER,timerHandler);
				_timer.stop();
			}
			_soundControl.stop();
			_soundControl = null;
			_sound = null;
		}
		
	}
}